<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="118px">
      <el-form-item label="仓库" prop="scId">
        <el-select v-model="queryParams.scId" placeholder="全部" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in ck" :key="index" :label="item.name"
                     :value="item.id" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品编号" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入商品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类目" prop="bcName">
        <el-select v-model="queryParams.bcName" placeholder="全部" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in category" :key="index" :label="item.name"
                     :value="item.id" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品品牌" prop="bbName">
        <el-select v-model="queryParams.bbName" placeholder="全部" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in brand" :key="index" :label="item.name"
                     :value="item.id" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型" prop="bizType">
        <el-select v-model="queryParams.bizType" placeholder="全部" clearable :style="{width: '100%'}">
          <el-option label="采购入库" value="1" ></el-option>
          <el-option label="采购退货出库" value="2" ></el-option>
          <el-option label="销售出库" value="3" ></el-option>
          <el-option label="销售退货入库" value="4" ></el-option>
<!--          <el-option label="盘点入库" value="7" ></el-option>-->
<!--          <el-option label="盘点出库" value="8" ></el-option>-->
<!--          <el-option label="库存成本调整" value="9" ></el-option>-->
<!--          <el-option label="库存调整" value="10" ></el-option>-->
<!--          <el-option label="仓库调拨" value="11" ></el-option>-->
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:log:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="logList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="ID" align="center" prop="id" width="180"/>-->
      <el-table-column label="仓库编号" align="left" prop="scCode" width="200px"/>
      <el-table-column label="仓库名称" align="left" prop="scName" width="100"/>
      <el-table-column label="商品编号" align="left" prop="productCode" width="200px"/>
      <el-table-column label="商品名称" align="left" prop="productName" width="130"/>
      <el-table-column label="商品类目" align="left" prop="bcName" width="100"/>
      <el-table-column label="商品品牌" align="left" prop="bbName" width="100"/>
      <el-table-column label="变动前库存数量" align="left" prop="oriStockNum" width="120"/>
      <el-table-column label="变动后库存数量" align="left" prop="curStockNum" width="120"/>
      <el-table-column label="变动库存数量" align="left" prop="stockNum" width="100"/>
      <el-table-column label="变动前含税成本价" align="left" prop="oriTaxPrice" width="130"/>
      <el-table-column label="变动后含税成本价" align="left" prop="curTaxPrice" width="130"/>
      <el-table-column label="变动含税金额" align="left" prop="taxAmount" width="100"/>
      <el-table-column label="操作人" align="left" prop="createBy" width="100"/>
      <el-table-column label="操作时间" align="left" prop="createTime" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务单据号" align="left" prop="bizCode" width="200"/>
      <el-table-column label="业务类型" align="left" prop="bizType" width="130">
        <template slot-scope="scope">
          <span>{{ scope.row.bizType == 1 ? '采购入库' : scope.row.bizType == 2 ? '采购退货出库' :
            scope.row.bizType == 3 ? '销售出库' : scope.row.bizType == 4 ? '销售退货入库' :
                scope.row.bizType == 7 ? '盘点入库' : scope.row.bizType == 8 ? '盘点出库' :
                  scope.row.bizType == 9 ? '库存成本调整' : scope.row.bizType == 10 ? '库存调整' : '仓库调拨'}}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品库存变动记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库" prop="scId">
          <el-select v-model="queryParams.scId" placeholder="全部" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in ck" :key="index" :label="item.name"
                       :value="item.id" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品ID" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入商品ID" />
        </el-form-item>
        <el-form-item label="原含税成本价" prop="oriTaxPrice">
          <el-input v-model="form.oriTaxPrice" placeholder="请输入原含税成本价" />
        </el-form-item>
        <el-form-item label="现含税成本价" prop="curTaxPrice">
          <el-input v-model="form.curTaxPrice" placeholder="请输入现含税成本价" />
        </el-form-item>
        <el-form-item label="原库存数量" prop="oriStockNum">
          <el-input v-model="form.oriStockNum" placeholder="请输入原库存数量" />
        </el-form-item>
        <el-form-item label="现库存数量" prop="curStockNum">
          <el-input v-model="form.curStockNum" placeholder="请输入现库存数量" />
        </el-form-item>
        <el-form-item label="库存数量" prop="stockNum">
          <el-input v-model="form.stockNum" placeholder="请输入库存数量" />
        </el-form-item>
        <el-form-item label="含税金额" prop="taxAmount">
          <el-input v-model="form.taxAmount" placeholder="请输入含税金额" />
        </el-form-item>
        <el-form-item label="创建人ID" prop="createById">
          <el-input v-model="form.createById" placeholder="请输入创建人ID" />
        </el-form-item>
        <el-form-item label="业务单据IdD" prop="bizId">
          <el-input v-model="form.bizId" placeholder="请输入业务单据IdD" />
        </el-form-item>
        <el-form-item label="业务单据号" prop="bizCode">
          <el-input v-model="form.bizCode" placeholder="请输入业务单据号" />
        </el-form-item>
        <el-form-item label="业务单据明细ID" prop="bizDetailId">
          <el-input v-model="form.bizDetailId" placeholder="请输入业务单据明细ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLog, getLog, delLog, addLog, updateLog } from "@/api/system/log";
import { getCategory, getBrand } from "@/api/system/stock";
import { center } from "@/api/system/order";
import {parseTime} from "../../../utils/ruoyi";

export default {
  name: "Log",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品库存变动记录表格数据
      logList: [],
      // 仓库下拉框
      ck: [],
      // 商品类目
      category: [],
      // 商品品牌
      brand: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scId: null,
        productId: null,
        productCode: null,
        productName: null,
        bcName: null,
        bbName: null,
        createMinTime: null,
        createMaxTime: null,
        bizType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        scId: [
          { required: true, message: "仓库ID不能为空", trigger: "blur" }
        ],
        productId: [
          { required: true, message: "商品ID不能为空", trigger: "blur" }
        ],
        oriTaxPrice: [
          { required: true, message: "原含税成本价不能为空", trigger: "blur" }
        ],
        curTaxPrice: [
          { required: true, message: "现含税成本价不能为空", trigger: "blur" }
        ],
        oriStockNum: [
          { required: true, message: "原库存数量不能为空", trigger: "blur" }
        ],
        curStockNum: [
          { required: true, message: "现库存数量不能为空", trigger: "blur" }
        ],
        stockNum: [
          { required: true, message: "库存数量不能为空", trigger: "blur" }
        ],
        taxAmount: [
          { required: true, message: "含税金额不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        createById: [
          { required: true, message: "创建人ID不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        bizType: [
          { required: true, message: "业务类型不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    /** 查询商品库存变动记录列表 */
    getList() {
      this.loading = true;
      listLog(this.queryParams).then(response => {
        this.logList = response.rows;
        console.log(this.logList)
        this.total = response.total;
        this.loading = false;
      });
      center(this.ck).then(response => {
        this.ck = response.rows;
      });
      getCategory().then(response => {
        this.category = response.rows;
      });
      getBrand().then(response => {
        this.brand = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        scId: null,
        productId: null,
        oriTaxPrice: null,
        curTaxPrice: null,
        oriStockNum: null,
        curStockNum: null,
        stockNum: null,
        taxAmount: null,
        createBy: null,
        createById: null,
        createTime: null,
        bizId: null,
        bizCode: null,
        bizDetailId: null,
        bizType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品库存变动记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLog(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品库存变动记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品库存变动记录编号为"' + ids + '"的数据项？').then(function() {
        return delLog(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/log/export', {
        ...this.queryParams
      }, `log_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
